package com.fdb.efp.console.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.log.LogManager;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.efp.console.service.facade.BizFlowInfoService;
import com.fdb.efp.console.service.facade.CopyBizFlowInfoService;
import com.fdb.efp.console.service.vo.BizFlowCfgTreeVO;
import com.fdb.efp.console.service.vo.BizFlowInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class BizFlowInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("bizFlowInfoService")
	private BizFlowInfoService bizFlowInfoService;

	@Autowired
	@Qualifier("copyBizFlowInfoService")
	private CopyBizFlowInfoService copyBizFlowInfoService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/flow/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowInfoVO>> queryBizFlowInfoAll(
			@RequestBody BizFlowInfoVO vo) {	
	    LogManager.info(vo.getBizFlowId(), bizFlowInfoService, "queryAll");
	    //List<BizFlowInfoVO> outputVo = this.executeQueryList("queryAll", bizFlowInfoService, new Object[]{vo});
	    List<BizFlowInfoVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		//Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		//DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		//setUserInfoToVO(vo);
		DataRuleType dataRuleType = DataRuleType.getDataRuleType("01");
		switch (dataRuleType) {
		case Owner:
			outputVo = bizFlowInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizFlowInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizFlowInfoService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizFlowInfoService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/flow/info/{bizFlowId}", method=RequestMethod.POST)
	public @ResponseBody ResponseData<BizFlowInfoVO> queryByPk(@PathVariable("bizFlowId") String bizFlowId) {
		BizFlowInfoVO inVo = new BizFlowInfoVO();
				inVo.setBizFlowId("FW20190820000000011");
		BizFlowInfoVO outputVo = bizFlowInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param bizFlowInfo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizFlowInfoVO inBizFlowInfoVo) {		
		int outputVo = bizFlowInfoService.deleteByPk(inBizFlowInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inBizFlowInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizFlowInfoVO inBizFlowInfoVo) {	
	    LogManager.info(inBizFlowInfoVo.getBizFlowId(), bizFlowInfoService, "updateByPk");
		int outputVo = bizFlowInfoService.updateByPk(inBizFlowInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inBizFlowInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizFlowInfo(@RequestBody BizFlowInfoVO inBizFlowInfoVo) {
		//setUserInfoToVO(inBizFlowInfoVo);
		int outputVo = bizFlowInfoService.insertBizFlowInfo(inBizFlowInfoVo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 复制一条数据
	 * @param inBizFlowInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/copy", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Boolean> CopyBizFlowInfo(@RequestBody BizFlowInfoVO inBizFlowInfoVo) {
        boolean outputVo = false;
        try {
            outputVo = copyBizFlowInfoService.copyOfBizFlowId(inBizFlowInfoVo.getBizFlowId(),inBizFlowInfoVo.getLegalOrgCode(),inBizFlowInfoVo.getCreateUser());
        } catch (Exception e) {
            logger.error("复制数据异常",e);
        }
        return getResponseData(outputVo);

	}
	
	/**
	 * 查询业务流转配置树
	 * @return
	 */
	@RequestMapping(value="/biz/flow/cfgTree", method=RequestMethod.GET)
	public @ResponseBody ResponseData<List<BizFlowCfgTreeVO<?>>> queryBizFlowCfgTree(@RequestParam String bizFlowId) {
		BizFlowInfoVO vo = new BizFlowInfoVO();
		vo.setBizFlowId(bizFlowId);
		
		List<BizFlowCfgTreeVO<?>> outputVo = bizFlowInfoService.queryBizFlowCfgTree(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 查询业务流转配置树
	 * @return
	 */
	@RequestMapping(value="/biz/flow/eventCfgTree", method=RequestMethod.GET)
	public @ResponseBody ResponseData<List<BizFlowCfgTreeVO<?>>> queryBizFlowEventCfgTree(@RequestParam String bizFlowId) {
		BizFlowInfoVO vo = new BizFlowInfoVO();
		vo.setBizFlowId(bizFlowId);
		
		List<BizFlowCfgTreeVO<?>> outputVo = bizFlowInfoService.queryBizFlowEventCfgTree(vo);
		return getResponseData(outputVo);
		
	}

}
